<?php
class Maintenance{
	
	private function __construct() {}
	
	/**
	 * Tabellen optimieren
	 *
	 * @param string $dbname Datenbankname
	 * @param string $pre Prefix
	 */
	public static function optimizeTables($dbname,$pre) {
		// Tabellen optimieren
		$tables = array();
		$output = "";
		
		// Get tables
		$i = 0;
		$getTables = DB::getInstance()->query("SHOW TABLES FROM $dbname LIKE '".$pre."%'");
		while($gT = DB::getInstance()->fetchRow($getTables)) {
			$tables[] = $gT[0];
			$i++;
		}
		mysql_free_result($getTables);
		
		$output = "Die Datenbank ".$dbname." enth&auml;hlt ".$i." Tabellen.<br />";
		
		// Optimize Tables
		foreach($tables as $table) {
			$optRes1 = DB::getInstance()->query("OPTIMIZE TABLE $table");
			$optRes = DB::getInstance()->fetchAssoc($optRes1);
			mysql_free_result($optRes1);
			
			$output .= "Optimiere ".$table."...erfolgreich.<br />";
		}
		
		$output .= "<br />Die Tabellen wurden erfolgreich optimiert.";
		
		General::ouputMessage("Erfolgreich","Die Tabellen wurden erfolgreich optimiert.","msg");
	}
	
	
	/**
	 * Tabellen-Backup erstellen
	 *
	 * @param string $dbname Datenbankname
	 * @param string $pre Prefix
	 * @param boolean $content Inhalt speichern?
	 */
	public static function backupTables($dbname,$pre,$content = true) {
		// Backup erstellen
		$tables = array();
		
		// Zum Download machen usw.
		header('Content-Type: application/octetstream');
		header('Content-Encoding: none');
		
		header('Content-Disposition: filename="'.$dbname.'_'.strftime('%Y-%m-%d').'.sql"');
		header('Cache-Control: private');
		header('Connection: close');
		
		// Header
		$crlf = "\r\n";
		
		echo
		'# ==========================================================', $crlf,
		'#', $crlf,
		'# Database dump of tables in `', $dbname, '`', $crlf,
		'# ',strftime('%Y-%m-%d'), $crlf,
		'#', $crlf,
		'# ==========================================================', $crlf,
		$crlf;
		
		// Tabellen mit dem Prefix auslesen
		$getTables1 = DB::getInstance()->query("SHOW TABLES FROM $dbname LIKE '".$pre."%'");
		while($gT = DB::getInstance()->fetchRow($getTables1)) {
			$tables[] = $gT[0];
		}
		mysql_free_result($getTables1);
		
		// Get the table structur
		foreach($tables as $table) {
			$tableStructur = "CREATE TABLE `$table` (".$crlf;
			
			// Structur
			$structur1 = DB::getInstance()->query("SHOW FIELDS FROM $table");
			while($s = DB::getInstance()->fetchArray($structur1)) {
				//print_r($s);
				$null = ($s['Null'] != 'YES') ? 'NOT NULL':'';
				$tableStructur .= '  `'.$s['Field'].'` '.$s['Type'].' '.$null;
				if (isset($row['Default'])) {
					$default = (is_numeric($s['Default'])) ? $s['Default']:"'".mysql_escape_string($s['Default'])."'";
					$tableStructur .= ' default '.$default;
				}
				
				$extra = ($s['Extra'] != '') ? ' '.$s['Extra'] : '';
				$tableStructur .= ' '.$extra.','.$crlf;
			}
			
			//Delete last komma
			$tableStructur = substr($tableStructur, 0, -strlen($crlf) - 1);
			$tableStructur .= $crlf;
			
			
			// get the key
			$keys1 = DB::getInstance()->query("SHOW KEYS FROM $table");
			while($k = DB::getInstance()->fetchArray($keys1)) {
				// IS this a primary key, unique index, or regular index?
				if($k['Key_name'] == "PRIMARY") {
					$key = 'PRIMARY KEY';
				}elseif(empty($k['Non_unique'])) {
					$key = 'UNIQUE';
				}elseif($k['Comment'] == 'FULLTEXT' || (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT')) {
					$key = 'FULLTEXT';
				}else{
					$key = 'KEY';
				}
				
				$tableStructur .= ','.$key.' (`'.$k['Column_name'].'`)';
				$tableStructur .= $crlf;
				

			}
			
			$tableStructur .= ' ) ENGINE=MyISAM;'.$crlf.$crlf;
			
			echo $tableStructur;
			
			if($content == true) {
				// get table content
				$tableContent = "";
				$content1 = DB::getInstance()->query("SELECT * FROM $table");
				if(DB::getInstance()->numRows($content1) > 0) {
					//tabellenfelder
					$fields1 = DB::getInstance()->query("SELECT * FROM $table LIMIT 1");
					$fields = array_keys(DB::getInstance()->fetchAssoc($fields1));
					
					$tableContent .= $crlf."INSERT INTO `$table` (";
					foreach($fields as $f) {
						$tableContent .= "`$f`,";
					}
					$tableContent = substr($tableContent, 0, -1);
					$tableContent .= ") VALUES";
					
					//tabellen zellen
					while($content = DB::getInstance()->fetchAssoc($content1)) {
						$tableContent .= $crlf."(";
						foreach($content as $key=>$value) {
							if(!isset($value)) $tableContent .= 'NULL';
							elseif(is_numeric($value)) $tableContent .= $value;
							else $tableContent .= "'" . mysql_escape_string($value) . "'";
							$tableContent .= ",";
						}
						$tableContent = substr($tableContent, 0, -1);
						$tableContent .= "),";
					}
					$tableContent = substr($tableContent, 0, -1);
					$tableContent .= ';'.$crlf.$crlf;
					
				}
				echo $tableContent;
			}
			
		}
		
		exit();
	}
}

?>